<%@ page contentType="text/html; charset=UTF-8"%>
<jsp:directive.page import="com.bstek.dorado.data.db.DBStatement"/>
<jsp:directive.page import="java.util.List"/>
<jsp:directive.page import="com.bstek.dorado.utils.variant.*"/>

<html>
	<head>
		<title>我的投票</title>
		<meta http-equiv="pragma" content="no-cache"> 
        <meta http-equiv="Cache-Control" content="no-cache, must-revalidate"> 
		<LINK href="../../../evjs/register.css" type=text/css rel=stylesheet> 
		<SCRIPT language=javascript src="../../../evjs/jquery-1.3.2.min.js"></SCRIPT>
		<SCRIPT language=javascript src="../../../evjs/psymatch.js" charset=utf-8></SCRIPT>
		<SCRIPT language=javascript  >
		 function getResult1(){
		 submit();
}
		function submit1(){
	var checked = checkBeforeSubmit();
	if(checked){ 
		$("#questionForm").submit();
//	parent.tabResult.setPath('getResult.jsp');
  //  parent.tabset1.addTab(parent.tabResult);
  //  parent.tabset1.setCurrentTab(parent.tabResult);
	}}
	 
	function setEleArr(){ 	
	alert();
	for(var i = 0 ; i < singleCheckArr.length ; i ++){
		var ele = singleCheckArr[i];
		var aa = $("input[name="+ele+"]:checked");
	
		if(aa.length !=0){
			$("#"+ele.split(".")[1]).addClass("blue");
		}
	}
}


function IsNumber(str)
{
   return str.match(/^[0-9]*$/)!=null;
}

var checkbox_array=new Array();
var min_num_array=new Array();

function CheckForm()
{
   var voteArray=document.form1.VOTE_IDS.value.split(",");
   for(i=0; i<voteArray.length;i++)
   {
      var obj=document.all("VOTE"+voteArray[i]);
      if(!obj)
         continue;
      
      for(j=0;j< obj.length;j++)
      { 
      	 if(obj.item(j).checked)
      	    break;
      }
      
      if(j==obj.length)
      {
      	 alert("请选择投票选项！");
         return (false);
      }
   }
   
   for(i=0; i<checkbox_array.length;i++)
   {
      var checked_count=0;
      var vote_id=checkbox_array[i];
      for (j=0; j< document.all("VOTE"+vote_id).length; j++)
      {
         if(document.all("VOTE"+vote_id).item(j).checked)
             checked_count++;
      }
      
      if(checked_count< min_num_array[i])//////////////////////////////
      {
         alert("第"+(i+1)+"个多选子投票 最少要选择"+ min_num_array[i]+"项！");
         return (false);
      }
   }
   
   for(i=0; i< document.form1.elements.length; i++)
   { 
      if(document.form1.elements[i].name.substr(0,10)!="VOTE_DATA_")
         continue;
      
      var name_array=document.form1.elements[i].name.split("_");
      var item = document.getElementById("ITEM"+name_array[2]);
      if(item && (item.type=="radio" || item.type=="checkbox") && !item.checked)
         continue;
      
      if(document.form1.elements[i].value=="")
      {
         alert("所有项目都必填！");
         document.form1.elements[i].focus();
         return false;
      }
      
      if(document.form1.elements[i].number && !IsNumber(document.form1.elements[i].value))
      {
         alert("请输入数字！");
         document.form1.elements[i].focus();
         return false;
      }
   }
   
   
   document.form1.submit();
}

function AddValue(vote_id,item_id,max_num)
{
 //alert();
   var item_id_str=document.form1.ITEM_ID.value;
   if(document.all("ITEM"+item_id).type=="checkbox" && max_num>0)
   {
      checked_count=0;
      for (i=0; i< document.all("VOTE"+vote_id).length; i++)
      {
         if(document.all("VOTE"+vote_id).item(i).checked)
            checked_count++;
         if(checked_count>max_num)
         {
            alert("最多只能选择"+max_num+"项！");
            document.all("ITEM"+item_id).checked=false;
            return;
         }
      }
  }
  else if(document.all("ITEM"+item_id).type=="radio")
  {
     for (i=0; i< document.all("VOTE"+vote_id).length; i++)
     {
         var radio_id = document.all("VOTE"+vote_id).item(i).value;
         if(item_id_str.indexOf(radio_id+",")==0)
   	        item_id_str=item_id_str.replace(radio_id+",","");
         else if(item_id_str.indexOf(","+radio_id+",")>0)
   	        item_id_str=item_id_str.replace(","+radio_id+",",",");
     }
  }
   
   if(item_id_str.indexOf(item_id+",")==0)
   	 item_id_str=item_id_str.replace(item_id+",","");
   else if(item_id_str.indexOf(","+item_id+",")>0)
   	 item_id_str=item_id_str.replace(","+item_id+",",",");
   else
      item_id_str+=item_id+",";
      
   if (document.all("ITEM"+item_id).type=="checkbox" && document.all("ITEM"+item_id).checked)
   document.all("HIDDEN_ITEM"+item_id).value=item_id;
   else if (document.all("ITEM"+item_id).type=="textarea" && document.all("ITEM"+item_id).value!='')
   document.all("HIDDEN_ITEM"+item_id).value=document.all("ITEM"+item_id).value;
   document.form1.ITEM_ID.value=item_id_str;
}

		</SCRIPT>
		
	</head>
	<body style="overflow-x: auto;overflow-y: auto">
 <div style="width: 100% height: 100%; overflow: auto">
 
<%
DBStatement dbs=new DBStatement();
String voteId= request.getParameter("VOTE_ID");

dbs.setSql("select * from oa_vote start with vote_id="+voteId+" connect by prior vote_id=super_vote_id ");
List list=dbs.queryForList();
 %>
		<DIV id=baihe_page> 
			<!--headEnd-->
			<DIV class=register_bj id=baihe_content>
				<DIV class=reg_lovetest>
				<%
				 VariantSet vs1=(VariantSet)list.get(0); 
				  %>
				  <%=vs1.getString("CONTENT")%>
					<FORM  name="form1" action=voteSubmit.jsp method='post' target=_self>
					  <INPUT type=hidden value=<%=voteId %> name=voteID/>
					  <input name="VOTE_ID" type="hidden" value="<%=voteId %>"/>
  					  <input name="ITEM_ID" id="ITEM_ID" type="hidden" value=""/>
						<DIV class=cont>
						<%
						String checkArr="",minArr="";
						for (int i=0;i<list.size();i++){
VariantSet vs=(VariantSet)list.get(i);
  if(vs.getString("VOTE_TYPE").equalsIgnoreCase("checkbox")){
   checkArr+=""+ vs.getInt("vote_id")+","; 
  minArr+=""+ vs.getInt("MIN_SELECT")+",";
 %>
 
 <DL id=SN<%=vs.getInt("vote_id")%>>
								<DT>
								   <%=vs.getString("subject") %>：
								</DT>	
							<% 
							dbs.setSql("select vs.*,rownum from oa_vote_select vs where vs.vote_id="+vs.getInt("vote_id")+" order by VISIBLE_ORDER desc");
							List listSub=dbs.queryForList();
							for (int j=0;j<listSub.size();j++){
								VariantSet vsSub=(VariantSet)listSub.get(j);
								out.println("<input name='HIDDEN_ITEM"+vsSub.getString("VOTE_SELECT_ID")+"' type='hidden' value=''/>");
							 %>
							 	 <DD><%=vsSub.getString("rownum") %>,
									<INPUT name='VOTE<%=vsSub.getString("VOTE_ID")%>' id='ITEM<%=vsSub.getString("VOTE_SELECT_ID")%>' style="CURSOR: pointer" 
									onclick="AddValue('<%=vsSub.getString("VOTE_ID")%>','<%=vsSub.getString("VOTE_SELECT_ID")%>','<%=vs.getInt("MAX_SELECT")%>')" type=checkbox value=<%=vsSub.getInt("VOTE_SELECT_ID") %> %>
									<LABEL style="CURSOR: pointer" for=ITEM<%=vsSub.getString("VOTE_SELECT_ID") %> >
										<%=vsSub.getString("select_content")%>
									</LABEL>
								</DD>	
							<%} %>							
 </DL>
 <% }else{
 %>
 
 <DL id=SN<%=vs.getInt("vote_id")%>>
								<DT>
								   <%=vs.getString("subject") %>：
								</DT>
								<% 
								dbs.setSql("select vs.*,rownum from oa_vote_select vs where vs.vote_id="+vs.getInt("vote_id")+" order by VISIBLE_ORDER desc");
							    List listSub=dbs.queryForList();
						    	for (int j=0;j<listSub.size();j++){
								VariantSet vsSub=(VariantSet)listSub.get(j);
								out.println("<input name='HIDDEN_ITEM"+vsSub.getString("VOTE_SELECT_ID")+"' type='hidden' value=''/>");
								%>
								
							 	 <DD><%=vsSub.getString("rownum") %>,<%=vsSub.getString("select_content")%><br>
							 	 <textarea name='VOTE_DATA_<%=vsSub.getString("VOTE_ID")%>' id='ITEM<%=vsSub.getString("VOTE_SELECT_ID")%>' onchange="AddValue('<%=vsSub.getString("VOTE_ID")%>','<%=vsSub.getString("VOTE_SELECT_ID")%>','<%=vs.getInt("MAX_SELECT")%>')"  cols="80" rows="2"></textarea>
							 	 
								</DD>
								<%} %>		
						 							
 </DL>
 <%
 } 
 } %>					 
						</DIV>
						<DIV class=btn>
							<DIV >
			 <input type="button" value="投票"   onClick="CheckForm();">&nbsp;&nbsp;
       
        <input type="button" value="关闭"  onClick="javascript:window.close();">
							</DIV>
							 
						</DIV>
			 <input name="VOTE_IDS" type="hidden" value="<%=checkArr.substring(0,checkArr.length()-1)%>"/>
					</FORM>
				</DIV>
			</DIV>
			<SCRIPT type=text/javascript>
		 
checkbox_array=Array(<%=checkArr.substring(0,checkArr.length()-1)%>);
min_num_array=Array(<%=minArr.substring(0,minArr.length()-1)%>);
	</SCRIPT>

		</DIV>
		
		</div>
	</body>
</html>
